﻿////////////////////////////////////////////////////////////////
/// Agus Kurniawan
/// aguskur@hotmail.com
/// http://blog.aguskurniawan.net
////////////////////////////////////////////////////////////////

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Kujang
{
    public partial class MainForm : Form
    {
        private KujangPort _kujang;
        private string _content;
        private int _endingIndex;

//No line ending  
//Newline (\n)
//Carriage return (\r)
//Both NL and CR (\r\n)

        public MainForm(KujangPortParam portParams)
        {
            InitializeComponent();
            cboEnding.SelectedIndex = 1;

            _kujang = new KujangPort(portParams);
            _kujang.ReceivedData += OnReceivedData;
            _kujang.ReceivedSignal += OnReceivedSignal;
            _kujang.Start();
        }

        private void OnReceivedSignal(object sender, EventArgs e)
        {
            string data = ((ReceivedDataEventArgs)e).Data;
            PerformSignal(data);
        }

        private void PerformSignal(string data)
        {
            try
            {
                if (InvokeRequired)
                {
                    this.Invoke(new Action<string>(PerformSignal), new object[] { data });
                    return;
                }
                if(data!="COMON" && data!="COMOFF")
                {
                    lbTX.BackColor = SystemColors.Control;
                    lbRX.BackColor = SystemColors.Control;
                    lbBREAK.BackColor = SystemColors.Control;
                    lbCD.BackColor = SystemColors.Control;
                    lbCTS.BackColor = SystemColors.Control;
                    lbDSR.BackColor = SystemColors.Control;
                    lbRING.BackColor = SystemColors.Control;
                    switch (data)
                    {
                        case "TX":
                            lbTX.BackColor = Color.LawnGreen;
                            break;
                        case "TXF":
                            lbTX.BackColor = SystemColors.Control;
                            break;
                        case "RX":
                            lbRX.BackColor = Color.LawnGreen;
                            break;
                        case "BREAK":
                            lbBREAK.BackColor = Color.LawnGreen;
                            break;
                        case "CD":
                            lbCD.BackColor = Color.LawnGreen;
                            break;
                        case "CTS":
                            lbCTS.BackColor = Color.LawnGreen;
                            break;
                        case "DSR":
                            lbDSR.BackColor = Color.LawnGreen;
                            break;
                        case "RING":
                            lbRING.BackColor = Color.LawnGreen;
                            break;
                    }
                }
                else
                {
                    if (data == "COMON")
                        lbStatus.Text = "COM6 connnected";
                    else
                        lbStatus.Text = "COM6 disconnected";
                }
                
            }
            catch (Exception) { }
            
        }

        private void AddLogMessage(string data)
        {
            try
            {
                if (InvokeRequired)
                {
                    this.Invoke(new Action<string>(AddLogMessage), new object[] { data });
                    return;
                }
                string content = "";
                int index = -1;
                switch(_endingIndex)
                {
                    case 0:
                        content = txtLog.Text;
                        content = string.Format("{0}{1}", content, data);
                        txtLog.Text = content;
                        break;
                    case 1:
                        index = data.IndexOf("\n");
                        if(index>=0)
                        {
                            content = txtLog.Text;
                            string tmp = data.Substring(0, index);
                            _content = string.Format("{0}{1}\r\n", _content, tmp);
                            txtLog.Text = string.Format("{0}{1}", content, _content);
                            _content = data.Substring(index + 1);
                        }
                        else
                            _content = string.Format("{0}{1}", _content, data);
                        break;
                    case 2:
                        index = data.IndexOf("\r");
                        if(index>=0)
                        {
                            content = txtLog.Text;
                            string tmp = data.Substring(0, index);
                            _content = string.Format("{0}{1}\r\n", _content, tmp);
                            txtLog.Text = string.Format("{0}{1}", content, _content);
                            _content = data.Substring(index + 1);
                        }
                        else
                            _content = string.Format("{0}{1}", _content, data);
                        break;
                    case 3:
                        index = data.IndexOf("\r\n");
                        if(index>=0)
                        {
                            content = txtLog.Text;
                            string tmp = data.Substring(0, index);
                            _content = string.Format("{0}{1}\r\n", _content, tmp);
                            txtLog.Text = string.Format("{0}{1}", content, _content);
                            _content = data.Substring(index + 1);
                        }
                        else
                            _content = string.Format("{0}{1}", _content, data);
                        break;
                }
                txtLog.SelectionStart = txtLog.Text.Length;
                txtLog.ScrollToCaret();
                
            }
            catch (Exception) { }
            
        }

        private void OnReceivedData(object sender, EventArgs e)
        {
            string data = ((ReceivedDataEventArgs)e).Data;
            AddLogMessage(data);
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            _kujang.Stop();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox frm = new AboutBox();
            frm.ShowDialog();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cboEnding_TextChanged(object sender, EventArgs e)
        {
            _endingIndex = cboEnding.SelectedIndex;
            if(!string.IsNullOrEmpty(_content))
            {
                string content = txtLog.Text;
                content = string.Format("{0}{1}", content, _content);
                txtLog.Text = content;
            }

        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            _kujang.SendMessage(txtMessage.Text);
        }
    }
}
